home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / xblockbuster / stage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  4.2 KB  |  162 lines

  1. /*
  2.  * File:       stage.c
  3.  * Author:     Eric Van Gestel
  4.  * Updated for X11 by Mark S. Wedel
  5.  *
  6.  * For:                xblockbuster
  7.  */
  8.  
  9. #include "xblockbuster.h"
  10.  
  11. void
  12. get_stage(  )
  13. {
  14.     FILE           *fd;
  15.     char            buf[MAX_COL + 3], stg[STAGEFILE_LENGTH];
  16.     register int    row, col, tmp;
  17.     register char   code;
  18.  
  19.     nbricks = 0;
  20.     score_incr = 1;
  21.     loop_nhits = 0;
  22.     last_busted_brick = NULL;
  23.  
  24.     /* open next stage file */
  25.     sprintf( stg, STAGEFILE, playground, stage_nb );
  26.     if ( !( fd = fopen( stg, "r" ) ) ) {
  27.         perror( "Can't open stage" );
  28.         exit( 1 );
  29.     }
  30.     /* clear msg, but leave the top line so the bonus remains on screen */
  31.     XFillRectangle( display,win, gc_erase,
  32.         font_height+5, STAGE_WIDTH_IN_PIXELS - 1, 
  33.         MSG_HEIGHT, STAGE_WIDTH_IN_PIXELS -1);
  34.  
  35.     /* read stage name */
  36.     fscanf( fd, "%s\n", stage_name );
  37.     for ( tmp = 0; stage_name[tmp]; tmp++ )
  38.         if ( stage_name[tmp] == '_' )
  39.             stage_name[tmp] = ' ';
  40.     for (  /* tmp = tmp */ ; tmp < NAME_LENGTH - 1; tmp++ )
  41.         stage_name[tmp] = ' ';
  42.     stage_name[NAME_LENGTH - 1] = '\0';
  43.     XDrawImageString(display, win, gc, OFFSET_SPEED, font_height*2,
  44.         stage_name, strlen(stage_name));
  45.  
  46.     /* read pallet dimensions */
  47.     fscanf( fd, "%d%d\n", &pallet_lengthI, &pallet_heightI );
  48.     if ( pallet_lengthI < SHORT_PALLET_LENGTH ||
  49.          pallet_lengthI > LONG_PALLET_LENGTH ||
  50.          pallet_heightI < pallet_lengthI ||
  51.          pallet_heightI > MAX_PALLET_HEIGHT ) {
  52.         perror( "Inconsistent pallet dimensions" );
  53.         exit( 1 );
  54.     }
  55.     /* modify for difficulty level */
  56.     pallet_lengthI -= ( pallet_modif * pallet_lengthI ) / PALLET_DENOMINATOR;
  57.     if ( pallet_lengthI < MIN_PALLET_LENGTH )
  58.         pallet_lengthI = MIN_PALLET_LENGTH;
  59.     if ( pallet_lengthI > MAX_PALLET_LENGTH )
  60.         pallet_lengthI = MAX_PALLET_LENGTH;
  61.     if ( pallet_heightI < pallet_lengthI )
  62.         pallet_heightI = pallet_lengthI;
  63.     pallet_length = ( double ) pallet_lengthI;
  64.     pallet_height = ( double ) pallet_heightI;
  65.  
  66.     /* read stage map */
  67.     for ( row = 0; row <= MAX_ROW; row++ ) {
  68.         if ( !fgets( buf, MAX_COL + 3, fd ) ) {
  69.             perror( "Can't read stage" );
  70.             exit( 1 );
  71.         }
  72.         for ( col = 0; col <= MAX_COL; col++ ) {
  73.             code = buf[col];
  74.             if ( IS_HIT_BRICK( code ) )
  75.                 nbricks++;
  76.             switch ( code ) {
  77.             case '/':
  78.                 launch_quadrant = NE;
  79.                 launch_row = row;
  80.                 launch_col = col;
  81.                 launch_x = ( double ) ( COL_X( col + 1 ) );
  82.                 launch_y = ( double ) ( ROW_Y( row ) );
  83.                 break;
  84.             case '\\':
  85.                 launch_quadrant = NW;
  86.                 launch_row = row;
  87.                 launch_col = col;
  88.                 launch_x = ( double ) ( COL_X( col ) );
  89.                 launch_y = ( double ) ( ROW_Y( row ) );
  90.                 break;
  91.             case '^':
  92.                 emit_row = row;
  93.                 emit_col = col;
  94.             }
  95.             stage[row][col].code = code;
  96.             stage[row][col].nhits = 0;
  97.         }
  98.     }
  99.     fclose( fd );
  100.     XClearWindow(display, win);
  101.  
  102.     /* draw new stage */
  103.     for ( row = 0; row <= MAX_ROW; row++ ) {
  104.         draw_brick0( row, 0 );
  105.         for ( col = 1; col < MAX_COL; col++ )
  106.             draw_brick( row, col );
  107.         draw_brick0( row, MAX_COL );
  108.     }
  109.  
  110.     /* reset pallet location */
  111.     pallet_y = ( double ) ( pallet_yI = PALLET_MAX_Y + 4 );
  112.     pallet_row = MAX_ROW - 1;
  113.     draw_pallet(  );
  114.  
  115.     /* ready ? */
  116.     XDrawImageString(display, win, gc, OFFSET_BALLS, font_height*2,
  117.           "Press right mouse button when ready; Escape to save.      ",60);
  118.  
  119. }
  120.  
  121. void
  122. new_stage(  )
  123. {
  124.     FILE           *fd;
  125.     register int    stage_index, stage_nb_tmp;
  126.     char        buf[STAGEFILE_LENGTH], buf2[2*STAGEFILE_LENGTH];
  127.  
  128.     /* go faster or make the pallet smaller */
  129.     if ( launch_speed < MAX_SPEED )
  130.         launch_speed += SPEED_INCR;
  131.     else
  132.         pallet_modif += PALLET_INCR;
  133.  
  134.     /* determine stage number */
  135.     if ( !nb_stages ) {
  136.         /* read number of available stages */
  137.         sprintf( buf, NB_STAGESFILE, playground );
  138.         if ( !( fd = fopen( buf, "r" ) ) ) {
  139.             sprintf( buf2, "Can't open number of stages file <%s>", buf);
  140.             perror( buf2 );
  141.             exit( 1 );
  142.         }
  143.         fscanf( fd, "%d", &nb_stages );
  144.         fclose( fd );
  145.         /* clear stages memory */
  146.         for ( stage_nb_tmp = 0; stage_nb_tmp < MAX_NB_STAGES; )
  147.             stages[stage_nb_tmp++] = FALSE;
  148.     }
  149.     /* search for stage index'th available stage number */
  150.     stage_index = ( int ) ( lrand48(  ) ) % nb_stages--;
  151.     if ( stage_index < 0 )
  152.         stage_index = -stage_index;
  153.     for ( stage_nb = 0; stages[stage_nb]; )
  154.         stage_nb++;
  155.     while ( stage_index-- ) {
  156.         while ( stages[++stage_nb] );
  157.     }
  158.     stages[stage_nb] = TRUE;
  159.  
  160.     get_stage(  );
  161. }
  162.